#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
using namespace std;
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_XClient/XClient_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_XClient/XClient_Error.h"

//g++ -std=gnu++11 -Wall -g Test_XClient.cpp -o Test_XClient -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia -lNetEngine_BaseLib -lStreamMedia_XClient -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_StreamMedia:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_LibEx,--disable-new-dtags

FILE* pSt_VFile;
FILE* pSt_AFile;
LPCTSTR lpszVFile = _T("480p.264");
LPCTSTR lpszAFile = _T("mix.aac");

int fread_video(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
    int nRet = fread(puszMsgBuffer, 1, nSize, pSt_VFile);
    if (nRet <= 0)
    {
        fclose(pSt_VFile);
        pSt_VFile = _tfopen(lpszVFile, "rb");
        nRet = fread(puszMsgBuffer, 1, nSize, pSt_VFile);
        return nRet;
    }
    return nRet;
}
int fread_audio(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
    int nRet = fread(puszMsgBuffer, 1, nSize, pSt_AFile);
    if (nRet <= 0)
    {
        fclose(pSt_AFile);
        pSt_AFile = _tfopen(lpszAFile, "rb");
        nRet = fread(puszMsgBuffer, 1, nSize, pSt_AFile);
        return nRet;
    }
    return nRet;
}

int Test_RTMPPush()
{
    XNETHANDLE xhStream = 0;
    LPCTSTR lpszUrl = _T("rtmp://192.168.4.37:1935/streamweb/qyt");
    BOOL bMemory = TRUE;

    if (bMemory)
    {
        pSt_VFile = _tfopen(lpszVFile, "rb");
        if (NULL == pSt_VFile)
        {
            printf("_tfopen:%d\n", errno);
            return -1;
        }
        pSt_AFile = _tfopen(lpszAFile, "rb");
        if (NULL == pSt_VFile)
        {
            printf("_tfopen:%d\n", errno);
            return -1;
        }

        if (!XClient_FilePush_Init(&xhStream))
        {
            printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
            return -1;
        }
        if (!XClient_FilePush_Input(xhStream, NULL, NULL, fread_video, fread_audio, NULL, NULL))
        {
            printf("XClient_FilePush_Input:%lX\n", XClient_GetLastError());
            //return -1;
        }
        if (!XClient_FilePush_Output(xhStream, lpszUrl))
        {
            printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
            return -1;
        }
        if (!XClient_FilePush_Start(xhStream))
        {
            printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
            return -1;
        }
    }
    else
    {
        if (!XClient_FilePush_Init(&xhStream))
        {
            printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
            return -1;
        }
        if (!XClient_FilePush_Input(xhStream, lpszUrl))
        {
            printf("XClient_FilePush_Input:%lX\n", XClient_GetLastError());
            return -1;
        }
        if (!XClient_FilePush_Output(xhStream, lpszUrl))
        {
            printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
            return -1;
        }
        if (!XClient_FilePush_Start(xhStream))
        {
            printf("XClient_FilePush_Output:%lX\n", XClient_GetLastError());
            return -1;
        }
    }

    BOOL bIsPush = TRUE;
    while (bIsPush)
    {
        XClient_FilePush_GetStatus(xhStream, &bIsPush);
        Sleep(1000);
    }
    XClient_FilePush_Close(xhStream);
    return 1;
}

int CBStream_Pull(LPVOID lParam, uint8_t* puszMsgBuffer, int nSize)
{
    FILE* pSt_File = (FILE*)lParam;
    printf("%d\n", nSize);
    int nRet = fwrite(puszMsgBuffer, 1, nSize, pSt_File);

    return nRet;
}
int Test_RTMPPull()
{
    XNETHANDLE xhStream = 0;
    LPCTSTR lpszUrl = _T("rtmp://www.xyry.org:1935/live/qyt");
    LPCTSTR lpszFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\stream.flv");

    FILE* pSt_File = _tfopen(lpszFile, _T("wb"));
    if (NULL == pSt_File)
    {
        printf("file:%d\n", errno);
        return -1;
    }

    if (!XClient_StreamPull_Init(&xhStream, lpszUrl, CBStream_Pull,pSt_File))
    {
        printf("XClient_FilePush_Push:%lX\n", XClient_GetLastError());
        return -1;
    }
    BOOL bPull = FALSE;
    XClient_StreamPull_GetStatus(xhStream, &bPull);

    while (bPull)
    {
        Sleep(1000);
    }
    XClient_StreamPull_Close(xhStream);

    return 1;
}

int main()
{
    Test_RTMPPush();
    //Test_RTMPPull();
    return 0;
}
